/*
 * Copyright (C) 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ch_linghu.fanfoudroid.data;

import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.ch_linghu.fanfoudroid.R;
import com.ch_linghu.fanfoudroid.TwitterApplication;
import com.ch_linghu.fanfoudroid.fanfou.PosterStatus;
import com.ch_linghu.fanfoudroid.fanfou.Status;
import com.ch_linghu.fanfoudroid.util.DateTimeHelper;
import com.ch_linghu.fanfoudroid.util.TextHelper;

public class Poster  implements Parcelable {
	private static final String TAG = "Poster";

	public com.ch_linghu.fanfoudroid.fanfou.User user;
	public String source;
	public String prevId;
	private int statusType = -1; // @see StatusTable#TYPE_*
	
	public String id;
	public String pid;
	public String type;
	public String uid;
	public String content;
	public String title;
	public Date deadline;
	public String isPrivate;
	public String cover;
	public Date ctime;
	public String posterType;
	

	public void setStatusType(int type) {
		statusType = type;
	}

	public int getStatusType() {
		return statusType;
	}

	public Poster() {
	}

	public static Poster create(PosterStatus status) {
		Poster poster = new Poster();

		poster.id = status.getId();
		poster.pid = status.getPid();
		poster.type = status.getType();
		
		// 转义符放到getSimpleTweetText里去处理，这里不做处理
		poster.content = TextHelper.getSimpleTweetText(status.getContent());
		poster.title = TextHelper.getSimpleTweetText(status.getTitle());
		poster.deadline = status.getDeadline();
		poster.ctime = status.getCtime();
//		tweet.favorited = status.isFavorited() ? "true" : "false";
//		tweet.truncated = status.isTruncated() ? "true" : "false";
		poster.isPrivate = status.getIsPrivate();
		poster.cover = status.getCover();
		 
		poster.posterType = status.getPosterType();

	 
	//	tweet.source = TextHelper.getSimpleTweetText(status.getSource());

		return poster;
	}

	public static Poster createFromSearchApi(JSONObject jsonObject)
			throws JSONException {
		Poster poster = new Poster();

		poster.id = jsonObject.getString("id");
		poster.pid = jsonObject.getString("pid");
		poster.type = jsonObject.getString("type");
		// 转义符放到getSimpleTweetText里去处理，这里不做处理
		poster.content = jsonObject.getString("content");
		poster.title = jsonObject.getString("title");
		poster.deadline = DateTimeHelper.parseSearchApiDateTime(jsonObject
				.getString("deadline"));
		poster.ctime = DateTimeHelper.parseSearchApiDateTime(jsonObject
				.getString("ctime"));
		//tweet.favorited = jsonObject.getInt("favorited");
		poster.isPrivate = jsonObject.getString("private");
		poster.cover = jsonObject.getString("cover");
		poster.posterType = jsonObject.getString("posterType");
	 

//		tweet.screenName = TextHelper.getSimpleTweetText(jsonObject
//				.getString("from_user"));
//		tweet.profileImageUrl = jsonObject.getString("profile_image_url");
 
//		tweet.source = TextHelper.getSimpleTweetText(jsonObject
//				.getString("source"));

		return poster;
	}

	public static String buildMetaText(StringBuilder builder, Date createdAt,
			String source, String replyTo) {
		builder.setLength(0);

		builder.append(DateTimeHelper.getRelativeDate(createdAt));
		builder.append(" ");

		builder.append(TwitterApplication.mContext
				.getString(R.string.tweet_source_prefix));
		builder.append(source);

		if (!TextUtils.isEmpty(replyTo)) {
			builder.append(" "
					+ TwitterApplication.mContext
							.getString(R.string.tweet_reply_to_prefix));
			builder.append(replyTo);
			builder.append(TwitterApplication.mContext
					.getString(R.string.tweet_reply_to_suffix));
		}

		return builder.toString();
	}

	// For interface Parcelable

	public int describeContents() {
		return 0;
	}
	
//	public String id;
//	public String pid;
//	public String type;
//	public String uid;
//	public String content;
//	public String title;
//	public Date deadline;
//	public String isPrivate;
//	public String cover;
//	public Date ctime;
//	public String posterType;

	public void writeToParcel(Parcel out, int flags) {
		out.writeString(id);
		out.writeString(pid);
		out.writeString(type); 
		out.writeString(uid);
		out.writeString(content);
		out.writeString(title);

		out.writeValue(deadline); // Date
		out.writeString(isPrivate);
		out.writeString(cover);
		out.writeValue(ctime); // Date
		out.writeString(posterType);
	 

//		out.writeString(source);
	}

	public static final Parcelable.Creator<Poster> CREATOR = new Parcelable.Creator<Poster>() {
		public Poster createFromParcel(Parcel in) {
			return new Poster(in);
		}

		public Poster[] newArray(int size) {
			// return new Tweet[size];
			throw new UnsupportedOperationException();
		}
	};

	public Poster(Parcel in) {
 
		id = in.readString();
		pid = in.readString();
		type = in.readString();
		uid = in.readString();
		content = in.readString();
		title = in.readString();
		deadline = (Date) in.readValue(Date.class.getClassLoader());
		isPrivate = in.readString();
		cover = in.readString();
		ctime = (Date) in.readValue(Date.class.getClassLoader());
//		favorited = in.readString();
		posterType = in.readString();
 
	}

	@Override
	public String toString() {
		return "Tweet [id=" + id + ", pid=" + pid + ", type="
				+ type + ", uid=" + uid + ", content="
				+ content + ", title=" + title + ", deadline="
				+ deadline + ", isPrivate=" + isPrivate + ", cover="
				+ cover + ", ctime=" + ctime
				+ ", type=" + type 
				+ ", posterType=" + posterType 
				+ "]";
	}

}
